package com.hxkj.service.userCenter.impl;

import com.github.pagehelper.PageInfo;
import com.hxkj.client.service.SystemMessageSendServiceClient;
import com.hxkj.client.service.UserInfoServiceClient;
import com.hxkj.domain.po.AuthUser;
import com.hxkj.domain.po.SysUser;
import com.hxkj.domain.po.SystemMessageSend;
import com.hxkj.service.userCenter.SystemMessageSendService;
import com.hxkj.token.utils.TokenUtils;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ParameterDataStruct;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.domain.to.PageTo;
import com.platform.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

@Service
@Slf4j
public class SystemMessageSendServiceImpl implements SystemMessageSendService{

    @Autowired
    private SystemMessageSendServiceClient smsClient;

    @Autowired
    private TokenUtils tokenUtils;

    @Override
    public void sendMessageToAll(SystemMessageSend systemMessageSend) throws Exception {
        AuthUser user = (AuthUser) tokenUtils.getTokenUserInfo();

        if(StringUtils.isBlank(systemMessageSend.getMessageType())){
            throw new CustomMessageException("消息分类不能为空！");
        }
        if(StringUtils.isBlank(systemMessageSend.getMessageContent())){
            throw new CustomMessageException("消息内容不能为空！");
        }
        if(StringUtils.isBlank(systemMessageSend.getMessageTitle())){
            throw new CustomMessageException("消息标题不能为空！");
        }
        systemMessageSend.setSenderAccount(user.getUserAccount());
        systemMessageSend.setSenderId(user.getUserId()+"");
        ResultMessageStruct result = smsClient.sendMessageToAll(systemMessageSend);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getMessage());
        }
    }

    @Override
    public SystemMessageSend getSystemMessageSendInfo(SystemMessageSend systemMessageSend) throws Exception {
        if(systemMessageSend.getMessageId() == null){
            throw new CustomMessageException("消息编号不能为空！");
        }
        ResultDataStruct<SystemMessageSend> result = smsClient.getSystemMessageSendInfo(systemMessageSend);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getMessage());
        }
        return result.getData();
    }

    @Override
    public PageInfo<SystemMessageSend> getList(SystemMessageSend systemMessageSend, PageTo pageParam) throws Exception {
        if (pageParam.getPageNum() == null || pageParam.getPageSize() == null) {
            pageParam.setPageNum(1);
            pageParam.setPageSize(10);
        }
        ParameterDataStruct<SystemMessageSend> parameterDataStruct = new ParameterDataStruct<>();
        parameterDataStruct.setData(systemMessageSend);
        parameterDataStruct.setPageTo(pageParam);
        return smsClient.getList(parameterDataStruct);
    }

    @Override
    public void deleteSendMessage(String messageIds) throws Exception {
        if(StringUtils.isBlank(messageIds)){
            throw new CustomMessageException("消息编号不能为空!");
        }
        ResultMessageStruct result = smsClient.deleteSendMessage(messageIds);
        if(result.getCode() != 200){
            throw new CustomMessageException(result.getMessage());
        }
    }
}

